package my.assistant.reminder;

import java.util.Calendar;

public class RemindVO
{
	public int hour = 8;
	public int minute;

	public boolean[] repeatDay = new boolean[]
	{ false, false, false, false, false, false, false }; // 7,1,2,3,4,5,6
	public String sound = "";
	public boolean enableVibrate = true;
	public boolean enableBsWeek = true;
	public boolean enable = true;

	// below is not store
	public long alarmTime;
	public int leftHour;
	public int leftMinute;

	public boolean repeatEveryDay()
	{
		for (Boolean b : repeatDay)
		{
			if (b == false)
				return false;
		}
		return true;
	}

	public void calculateAlarmTime(Calendar c)
	{
		if (c == null)
		{
			c = Calendar.getInstance();
		}
		c.clear();
		Calendar now = Calendar.getInstance();
		int nowDay = now.get(Calendar.DAY_OF_WEEK);
		boolean todayAlarm = repeatDay[nowDay == 1 ? 6 : nowDay - 1];
		boolean tomorrowAlarm = repeatDay[nowDay == 7 ? 0 : nowDay];
		int nowHour = now.get(Calendar.HOUR_OF_DAY);
		int nowMinute = now.get(Calendar.MINUTE);
		if ((now.get(Calendar.HOUR_OF_DAY) * 100 + now.get(Calendar.MINUTE)) >= (hour * 100 + minute))
		{
			if (tomorrowAlarm)
			{
				// tomorrow
				nextDay(c, now);
			} else
			{
				// next day
				nowDay++;
				while (repeatDay[nowDay == 7 ? 0 : nowDay])
				{
					nextDay(c, now);
					break;
				}
			}
		} else
		{
			if (todayAlarm)
			{
				// today
				c.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR));
				c.set(Calendar.YEAR, now.get(Calendar.YEAR));
			}
		}
		c.set(Calendar.HOUR_OF_DAY, hour);
		c.set(Calendar.MINUTE, minute);
		nowHour = c.get(Calendar.HOUR_OF_DAY) - nowHour;
		leftHour = nowHour < 0 ? (nowHour + 24) : nowHour;
		nowMinute = c.get(Calendar.MINUTE) - nowMinute;
		leftMinute = nowMinute < 0 ? (nowMinute + 60) : nowMinute;
		this.alarmTime = c.getTimeInMillis();
	}

	private static void nextDay(Calendar c, Calendar now)
	{
		now.roll(Calendar.DAY_OF_YEAR, true);
		if (now.get(Calendar.DAY_OF_YEAR) == now.getActualMinimum(Calendar.DAY_OF_YEAR))
		{
			// next year
			now.roll(Calendar.YEAR, true);
		}
		c.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR));
		c.set(Calendar.YEAR, now.get(Calendar.YEAR));
	}
}
